<%@ page contentType="text/html; charset=UTF-8" language="java"%>
<%@ page import="com.hzqy.commons.utils.ConstantUtils" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>标签管理</title>
<link rel="stylesheet" type="text/css"
	href="../static/js/themes/defalut.css">
<link rel="stylesheet" type="text/css"
	href="../static/js/themes/public.css">
<link rel="stylesheet" type="text/css"
	href="../static/js/themes/ddspstyle.css">
<link rel="stylesheet" type="text/css"
	href="../static/js/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="../static/js/themes/icon.css">
<script type="text/javascript" src="../static/js/jquery.min.js"></script>
<script type="text/javascript" src="../static/js/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="../static/js/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="../static/js/jquery.validate.js"></script>
<script type="text/javascript" src="../static/js/index.js"></script>
<link rel="stylesheet" type="text/css"
	href="../static/zTree/css/zTree.css">
<link rel="stylesheet" type="text/css"
	href="../static/zTree/css/zTreeStyle/zTreeStyle.css">
<link rel="stylesheet" type="text/css"
	href="../static/js/themes/default/easyui.css">
<script type="text/javascript"
	src="../static/zTree/js/jquery.ztree.core.js"></script>
<script type="text/javascript"
	src="../static/zTree/js/jquery.ztree.excheck.js"></script>
<script type="text/javascript"
	src="../static/zTree/js/jquery.ztree.exedit.js"></script>
	<%
	String []nonceObj=ConstantUtils.getNonceObject();
	%>
<script type="text/javascript">
var isAdd = true;
var setting = {
		view: {
			addHoverDom: addHoverDom,
			removeHoverDom: removeHoverDom,
			selectedMulti: false
		},
		edit: {
			enable: true,
			editNameSelectAll: true,
			showRemoveBtn: false,
			showRenameBtn: false
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback: {
			beforeDrag: beforeDrag,
			onClick:zTreeOnClick,
			onAsyncSuccess: function(event, treeId, treeNode){
				
				
			}
		}
	};
	//禁止拖拽
	function beforeDrag(treeId, treeNodes) {
		return false;
	}
	//点击事件
	function zTreeOnClick(event,treeId,treeNode){
		if(treeNode.id !=0)
			window.parent.rightFrame.location.href = "label_info.jsp?parentid="+treeNode.id;
	}
	function nodeRemove(treeId, treeNode) {
		$.messager.confirm("操作提示","您确定要删除"+treeNode.name+" 吗？",function(sure){
			if(sure) {
				$.ajax({
					url:"delete_label.do?f_label_id="+treeNode.id+"&stime=<%=nonceObj[0]%>&sign=<%=nonceObj[1]%>",
					success:function(data) {
						data = JSON.parse(data);
						if(data.success){
							$.messager.alert("操作提示","操作成功!","info",function(){
								window.location.reload();
							});
						} else {
							$.messager.alert("操作提示","操作失败!","error");
						}
						window.location.reload();
					}
				});
			}
		});
		return false;
	}
	function addHoverDom(treeId, treeNode) {
		if(!treeNode.isParent) {
			var sObj2 = $("#" + treeNode.tId + "_span");
			if (treeNode.editNameFlag || $("#removeBtn_"+treeNode.tId).length>0) return;
			var removeStr = "<span class='button remove' id='removeBtn_" + treeNode.tId
				+ "' title='remove node' onfocus='this.blur();'></span>";
			sObj2.after(removeStr);
			var btn = $("#removeBtn_"+treeNode.tId);
			if (btn) btn.bind("click", function(event){
				nodeRemove(treeId, treeNode);
				event.stopPropagation();
			});
		}
		
		var sObj1 = $("#" + treeNode.tId + "_span");
		if (treeNode.editNameFlag || $("#editBtn_"+treeNode.tId).length>0) return;
		var editStr = "<span class='button edit' id='editBtn_" + treeNode.tId
			+ "' title='edit node' onfocus='this.blur();'></span>";
		sObj1.after(editStr);
		var btn = $("#editBtn_"+treeNode.tId);
		if (btn) btn.bind("click", function(event){
			window.parent.rightFrame.location.href = "label_edit.jsp?parentid="+treeNode.id;
			event.stopPropagation();
		});
		
		var sObj = $("#" + treeNode.tId + "_span");
		if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
		var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
			+ "' title='add node' onfocus='this.blur();'></span>";
		sObj.after(addStr);
		var btn = $("#addBtn_"+treeNode.tId);
		if (btn) btn.bind("click", function(event){
			window.parent.rightFrame.location.href = "label_add.jsp?parentid="+treeNode.id;
			event.stopPropagation();		
		});
	};
	function removeHoverDom(treeId, treeNode) {
		$("#addBtn_"+treeNode.tId).unbind().remove();
		$("#editBtn_"+treeNode.tId).unbind().remove();
		$("#removeBtn_"+treeNode.tId).unbind().remove();
	};
	function selectAll() {
		var zTree = $.fn.zTree.getZTreeObj("lefttree");
		zTree.setting.edit.editNameSelectAll =  $("#selectAll").attr("checked");
	}
	$(document).ready(function(){
		$("#right").hide();
		$.ajax({
			url:"label_list.do?f_label_id=0",
			success:function(data){
				zNodes = JSON.parse(data);
				var root = {id:"0",name:"标签目录",open:true};
				zNodes.push(root);
				var treeObj = $.fn.zTree.init($("#lefttree"), setting, zNodes);
				$("#selectAll").bind("click", selectAll);
				updateNoParentNode(treeObj);
			}
		});
	});
	
	function updateNoParentNode(treeObj){
		var nodesArr = treeObj.transformToArray(treeObj.getNodes());
		for(var i=0;i<nodesArr.length;i++){
			var rootNode = null;
			if(nodesArr[i].pId == null && nodesArr[i].id == '0'){
				rootNode = nodesArr[i];
				break;
			}
		}
		for(var i=0;i<nodesArr.length;i++){
			if(nodesArr[i].pId == null && nodesArr[i].id != '0'){
				treeObj.moveNode(treeObj.moveNode(rootNode, nodesArr[i], "inner"));
			}
		}
	}
	
</script>
</head>
<body style="background: #FFFFFF;">
	<div id="left" style="width: 100%; overflow: auto; float: left;">
		<ul id="lefttree" class="ztree"></ul>
	</div>
</body>
</html>